package com.hnyfkj.jyindustry.biz.sysmenu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.entity.SysMemberInfoEntity;
import com.hnyfkj.jyindustry.biz.sysmemberinfo.sevice.SysMemberInfoService;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuInsertDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuTreeDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuUpdateDto;
import com.hnyfkj.jyindustry.biz.sysmenu.entity.SysMenuEntity;
import com.hnyfkj.jyindustry.biz.sysmenu.service.SysMenuService;
import com.hnyfkj.jyindustry.common.utils.Response;
import com.hnyfkj.jyindustry.common.utils.ShiroUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zjq
 *
 * @Description
 *
 * @date 2020年12月10日
 */
@Controller
@RequestMapping("sysMenu")
@Api(tags = { "菜单以及首页相关" })
public class SysMenuController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SysMemberInfoService sysMemberInfoService;

	/*
	 * 查看菜单
	 */
	@GetMapping("/menu")
	@ResponseBody
	public Map<String, Object> menu() {
		SysMemberInfoEntity sysMemberInfoEntity = sysMemberInfoService
				.getById((String) ShiroUtils.getSessionAttribute("memberId"));
		return sysMenuService.menu(sysMemberInfoEntity);
	}

	/*
	 * 清除缓存
	 */
	@GetMapping("/clear")
	@ResponseBody
	public Map<String, Object> clear() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 1);
		map.put("msg", "服务端清理缓存成功");
		return map;
	}

	/*
	 * 主页设置
	 */
	@GetMapping("/welcome")
	public String welcome() {
		return "welcome";
	}

	/*
	 * 菜单列表页面
	 */
	@GetMapping("/listPage")
	public String listPage() {
		return "sys_menu/menu-table";
	}

	/*
	 * 菜单列表
	 */
	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value = "菜单列表")
	public JSONObject list() {
		JSONObject json = sysMenuService.menuList();
		return json;
	}

	/*
	 * 角色查看菜单列表
	 */
	@GetMapping("/menuTreelist")
	@ResponseBody
	@ApiOperation(value = "菜单列表")
	public JSONObject menuTreelist(@RequestParam("id") String roleId,@RequestParam("isMenu") Integer isMenu) {
		JSONObject json = sysMenuService.menuTreeList(roleId,isMenu);
		return json;
	}

	/*
	 * 添加菜单页面
	 */
	@GetMapping("/add")
	public String add() {
		return "sys_menu/menu-add";
	}

	/**
	 * 编辑页面
	 */
	@GetMapping("/edit")
	public String editPage(@RequestParam("id") Integer id, Model model) {
		SysMenuEntity sysMenuEntity = sysMenuService.getById(id.toString());
		model.addAttribute("sysMenuEntity", sysMenuEntity);
		return "sys_menu/menu-edit";
	}

	/*
	 * 菜单列表
	 */
	@PostMapping("/roleMenuList")
	@ResponseBody
	@ApiOperation(value = "菜单列表")
	public List<SysMenuEntity> roleMenuList(Integer parentId) {
		List<SysMenuEntity> list = sysMenuService.roleMenuList(parentId);
		return list;
	}

	/*
	 * 编辑加载菜单列表
	 */
	@PostMapping("/parentIdList")
	@ResponseBody
	@ApiOperation(value = "编辑加载菜单列表")
	public JSONObject parentIdList(Integer parentId) {
		SysMenuEntity sysMenuEntity = sysMenuService.getById(parentId);
		JSONObject json = sysMenuService.parentIdList(sysMenuEntity);
		return json;
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", produces = "application/json")
	@ResponseBody
	public Response save(@RequestBody final SysMenuInsertDto sysMenu) {
		sysMenuService.insert(sysMenu);
		return Response.success();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", produces = "application/json")
	@ResponseBody
	public Response update(@RequestBody final SysMenuUpdateDto sysMenu) {
		sysMenuService.update(sysMenu);
		return Response.success();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除")
	@ResponseBody
	public Response delete(@RequestParam("ids[]") final Integer[] menuIds) {
		sysMenuService.delectBatch(menuIds);
		return Response.success();
	}
	
	/**
	 * 角色添加菜单树形结构
	 */
	@PostMapping("/getRoleMenu")
	@ApiOperation(value = "编辑角色查菜单")
	@ResponseBody
	public List<SysMenuTreeDto> getRoleMenu(@RequestParam("id") final String roleId) {
		List<SysMenuTreeDto> list=sysMenuService.getRoleMenu(roleId);
		return list;
	}
}
